<?php
// @formatter:off
/**
 * @file WCToolsManager.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-05 00:31:35 +0000 (Wed, 05 Jun 2013) $
 *
 * $Id: WCToolsManager.php 69 2013-06-05 00:31:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/WCToolsManager.php $
 */
// @formatter:on

require_once dirname(__FILE__)."/WCTool.php";

/**
 * @class WCToolsManager
 */
class WCToolsManager {
	/**
	 * This is the sole instance for this singleton class.
	 * @var WCToolsManager
	 */
	protected static $_Instance;
	/**
	 * @var int
	 */
	protected $_toolId = false;
	/**
	 * @var bool
	 */
	protected $_showHelp = false;

	protected function __construct() {
		$this->_toolId = false;
		$this->_showHelp = false;

	}
	public function __destruct() {
	}
	/**
	 * Prevent users from clone the singleton's instance.
	 */
	public function __clone() {
		trigger_error(__CLASS__.": Clone is not allowed.", E_USER_ERROR);
	}
	//
	// Public methods
	public function run($spacer = "") {
		$this->loadParams();

		if($this->_showHelp || !$this->_toolId) {
			$this->showHelp();
		} else {
			global $wcToolScripts;
			global $wcOptions;

			$ok = false;

			$log = WCLog::Get(WC_LOGNAME_TOOL);
			$log->set();

			if(!$this->validateTool($this->_toolId)) {
				$log->write("{$spacer}".WCColorText::ShellYellow("Warning").": Configuration for ID '{$this->_toolId}' seems to be invalid", WCLogLevel::Warning);
				if(isset($wcToolScripts[$this->_toolId]["name"])) {
					$log->write(" (name: '{$wcToolScripts[$this->_toolId]["name"]}')\n");
				} else {
					$log->write("\n");
				}
			} elseif(!$wcToolScripts[$this->_toolId]["enabled"]) {
				$log->write("{$spacer}".WCColorText::ShellYellow("Warning").": Configuration for ID '{$this->_toolId}' seems to be disabled", WCLogLevel::Warning);
				if(isset($wcToolScripts[$this->_toolId]["name"])) {
					$log->write(" (name: '{$wcToolScripts[$this->_toolId]["name"]}')\n");
				} else {
					$log->write("\n");
				}
			} else {
				$ok = true;
			}

			if($ok) {
				if(isset($wcToolScripts[$this->_toolId]["path"])) {
					require_once $wcToolScripts[$this->_toolId]["path"];
				}

				if(!class_exists($wcToolScripts[$this->_toolId]["class"])) {
					$log->write("{$spacer}".WCColorText::ShellRed("Error").": Class '{$wcToolScripts[$this->_toolId]["class"]}' for tool '{$this->_toolId}' does not exists.\n", WCLogLevel::Fatal);
					die ;
				}

				$log->write($spacer.WCColorText::ShellGreen($wcToolScripts[$this->_toolId]["name"].":")."\n", WCLogLevel::Info);

				$wcOptions->clear();

				$tool = new $wcToolScripts[$this->_toolId]["class"]();
				$tool->run("{$spacer}\t");
				unset($tool);
			}
		}

	}
	public function showHelp($spacer = "") {
		global $wcToolScripts;

		if($this->_toolId !== false) {
			if($this->validateTool($this->_toolId)) {
				echo $spacer.WCColorText::ShellGreen($wcToolScripts[$this->_toolId]["name"].":")."\n";
				if(isset($wcToolScripts[$this->_toolId]["path"])) {
					require_once $wcToolScripts[$this->_toolId]["path"];
				}
				$tool = new $wcToolScripts[$this->_toolId]["class"]();
				$tool->showHelp("{$spacer}\t");
				unset($tool);
			} else {
				$log->write("".WCColorText::ShellRed("Error").": There is no tool with id '{$this->_toolId}' or maybe it is invalid.\n", WCLogLevel::Error);
			}
		} else {
			echo "Usage:\n";
			echo "\tphp tool.php [options]\n";

			echo "\nOptions:\n";
			echo "\t-h, --help\n";
			echo "\t\tShows this help text.\n\n";
			echo "\t-T id, --tool id\n";
			echo "\t\tRuns the specified tool. See 'Available Tools' below.\n";

			echo "\nAvailable Tools:\n";
			foreach($wcToolScripts as $id => $tool) {
				if($this->validateTool($id)) {
					echo "\t{$id}:\t{$tool["name"]}";
					echo isset($wcToolScripts[$id]["description"]) ? ": {$wcToolScripts[$id]["description"]}" : "";
					echo !$wcToolScripts[$id]["enabled"] ? " (".WCColorText::ShellYellow("Disabled").")" : "";
					echo ".\n";
				}
			}
		}
	}
	//
	// Protected methods
	protected function hasTool($id) {
		global $wcToolScripts;
		return isset($wcToolScripts[$id]);
	}
	/**
	 * This methods loads parameters and flags sets that may be needed for
	 * execution.
	 */
	public function loadParams() {
		global $wcOptions;

		if($wcOptions->addOption("Help")) {
			$wcOptions->addOptionTrigger("Help", "h");
			$wcOptions->addOptionTrigger("Help", "help");
		}
		if($wcOptions->addValueOption("Tool")) {
			$wcOptions->addOptionTrigger("Tool", "T");
			$wcOptions->addOptionTrigger("Tool", "tool");
		}

		$wcOptions->check();

		if($wcOptions->activated("Tool")) {
			$this->_toolId = $wcOptions->value("Tool");
		}
		if($wcOptions->activated("Help")) {
			$this->_showHelp = true;
		}
	}
	protected function login() {
		global $wcUser;
		global $wcSession;

		$wcUser = new WCUser();
		$wcUser->load(WC_USER_ROOT_ID);
		$wcSession->login(WC_USER_ROOT_ID);
	}
	protected function logout() {
		global $wcUser;
		global $wcSession;

		$wcUser = false;
		$wcSession->logout();
	}
	protected function validateTool($id) {
		global $wcToolScripts;

		return ($this->hasTool($id) && isset($wcToolScripts[$id]["class"]) && isset($wcToolScripts[$id]["enabled"]) && ((isset($wcToolScripts[$id]["path"]) && is_readable($wcToolScripts[$id]["path"])) || !isset($wcToolScripts[$id]["path"])));
	}
	//
	// Public class methods
	/**
	 * This class-method creates and returns the sole instance for this
	 * singleton class.
	 * @return WCToolsManager Sigleton instance.
	 */
	public static function Instance() {
		if(!isset(self::$_Instance)) {
			$c = __CLASS__;
			self::$_Instance = new $c();
		}

		return self::$_Instance;
	}
}
?>